package com.app.common.viewmodel

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel

open class LifecycleViewModel(private val lifecycle: Lifecycle) : ViewModel()  {
    init {
        lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                onLifecycleChange(event)
                when (event) {
                    Lifecycle.Event.ON_DESTROY -> {
                        release()
                        source.lifecycle.removeObserver(this)
                    }
                }
            }
        })
    }

    open fun release(){

    }

    protected open fun onLifecycleChange(event: Lifecycle.Event){

    }
}